﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Downloader.Framework.Core;

namespace Downloader.Framework.Queries
{
    public static class QueryExtensions
    {
        public static RecordList<T> GetList<T>(this IQueryable<T> q, QueryParamsBase query) where T : EntityBase
        {
            return GetList<T, int>(q, query);
        }

        public static RecordList<T> GetList<T, TID>(this IQueryable<T> q, QueryParamsBase query) where T : EntityBase<TID>
        {
            q = query.Get<T, TID>(q);
            var start = query.Start ?? 0;
            var count = query.Limit ?? 0;
            return GetList(q, count, start);
        }
        public static RecordList<T> GetList<T>(this IQueryable<T> q, int count, int start)
        {
            var records = new RecordList<T>
            {
                RecordCount = q.Count()
            };

            IEnumerable<T> data;
            if (records.RecordCount == 0)
            {
                data = new T[0];
            }
            else
            {
                //pagesize大于0时按页获取,等于0时取全部
                if (count > 0)
                {
                    data = start > 0
                        ? q.Skip(start).Take(count)
                        : q.Take(count);
                }
                else
                {
                    data = q;
                }
            }
            records.Data = data.ToList();
            return records;
        }

        public static RecordList<TS> GetModels<T, TS>(this IQueryable<T> q, QueryParamsBase query)
            where T : EntityBase<int>
            where TS : IEntityConvert<T>, new()
        {
            return GetModels<T, int, TS>(q, query);
        }

        public static RecordList<TS> GetModels<T, TID, TS>(this IQueryable<T> q, QueryParamsBase query)
            where T : EntityBase<TID>
            where TS : IEntityConvert<T>, new()
        {
            q = query.Get<T, TID>(q);
            var records = new RecordList<TS> { RecordCount = q.Count() };
            IList<TS> data;
            if (records.RecordCount == 0)
            {
                data = new TS[0];
            }
            else
            {
                if (query.Limit > 0)
                {
                    data = query.Start > 0
                           ? q.Skip(query.Start ?? 0).Take(query.Limit ?? 0).From<T, TID, TS>()
                           : q.Take(query.Limit ?? 0).From<T, TID, TS>();
                }
                else
                {
                    data = q.From<T, TID, TS>();
                }
            }
            records.Data = data;
            return records;
        }

        public static IEnumerable<T> GetTop<T, Tid>(this IQueryable<T> q, QueryParamsBase query)
            where T : EntityBase<Tid>
        {
            return query.Get<T, Tid>(q).Take(query.Limit ?? 1);
        }

        public static IEnumerable<TS> GetTopViewModels<T, TS>(this IQueryable<T> q, QueryParamsBase query)
            where T : EntityBase<int>
            where TS : IEntityConvert<T>, new()
        {
            return query.Get<T, int>(q).Take(query.Limit ?? 1).From<T, int, TS>();
        }

    }
}
